import Opt from '@/components/dt/info/Opt.js'
import ruleOpt from '@/biz/option/rule.js'
import organizationManagementMgr from '@/biz/organizationManagement/organizationManagement'

let OrgInfo = null
class NewOpt extends Opt {

  constructor(info) {
    super(info)
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.part1)
  }

  //循环遍历部门
  forEachDepartment(mess) {
    mess.map((item) => {
      item.key = item.id
      item.value = item.orgName
      item.children && this.forEachDepartment(item.children)
    })
    return mess
  }
  getOrgThreeTree() {
    let params = {
      isTwoLevel: true,
    }
    return organizationManagementMgr.getOrg(params).then(res => {
      if (res) {
        let list = res && res[0].children
        this.forEachDepartment(list)
        OrgInfo = this.forEachDepartment(list)
        return list
      }
    })
  }
  changeTree(level, id) {
    let arr = []
    if (level == 1) {
      return OrgInfo.filter(item => {
        return item['key'] == id
      })
    } else if (level == 2) {
      console.log(level, id)
      OrgInfo.forEach((item) => {
        if (item['key'] == this.info.orgDivisionId) {
          item.children.forEach(itemL => {
            if (itemL['key'] == id) {
              arr = itemL.children
            }
          })
        }
      })
      return arr
    }
  }
  getCompany() {
    let orgDivisionId = {
      title: '关联事业部',
      key: 'orgDivisionId',
      load: (o) => {
        this.getOrgThreeTree().then(res => {
          console.log(res, this.info.orgCompanyId, '123***')
          o.select.options = res
          if (this.info.orgDivisionId) {
            orgCompanyId.select.options = this.changeTree(1, this.info
              .orgDivisionId)[0].children
            if (this.info.orgCompanyId) {
              orgOfficeId.select.options = this.changeTree(2, this.info
                .orgCompanyId, )
            }
          }
        })
      },
      select: {
        options: [],
        clearable: true
      },
      change: (data) => {
        console.log(data, 'datadata')
        this.info.orgCompanyId = null
        this.info.orgOfficeId = null
        if (data.value) {
          orgCompanyId.select.options = this.changeTree(1, data.value)[0].children
        } else {
          orgCompanyId.select.options = []
        }
      },
    }
    let orgCompanyId = {
      title: '关联分公司',
      key: 'orgCompanyId',
      select: {
        options: [],
        clearable: true,
        multiple: true,
        maxTagCount: 2
      },
      change: (data) => {
        if (data.value && data.value.length == 1) {
          orgOfficeId.disabled = false
          orgOfficeId.select.options = this.changeTree(2, data.value)
        } else {
          orgOfficeId.disabled = true
          this.info.orgOfficeId = null
          orgOfficeId.select.options = []
        }
      },
    }
    let orgOfficeId = {
      title: '关联事业处',
      key: 'orgOfficeId',
      disabled: false,
      select: {
        options: [],
        clearable: true
      },
    }
    let opt = [orgDivisionId, orgCompanyId, orgOfficeId]
    return opt
  }
  createAll() {
    this.part1 = [{
        title: '展示名称',
        placeholder: '请输入展示名称',
        key: 'companyName',
        col: 20,
        input: { maxlength: 20 },
        rules: [
          ruleOpt.required('请输入展示名称')
        ]
      },
      ...this.getCompany(),
      {
        title: '展示地址',
        key: 'address',
        col: 20,
        input: { maxlength: 30 },
      },
      {
        title: '水印',
        key: 'waterMark',
        radio: {
          options: [{ key: '1', value: '开启' }, { key: '2', value: '关闭' }],
        },
        rules: [{
          required: true,
          message: '请选择水印',
          trigger: 'blur'
        }]
      }
    ]
  }
}

export default NewOpt